package com.cal2sync;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.Animation.AnimationListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.Spinner;

public class update_rule extends Activity{
	LinearLayout l;
	//EditText event_type;
	Spinner spinner, event_spinner;
	String event,action;
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.update_rule);

		event=getIntent().getStringExtra("event");
		action=getIntent().getStringExtra("action");
		
		spinner = (Spinner) findViewById(R.id.action_spinner);
		ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
				this, R.array.action_array, android.R.layout.simple_spinner_item);
		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		spinner.setAdapter(adapter);

		for(int i=0;i<spinner.getCount();i++)
		{
			if(spinner.getItemAtPosition(i).toString().equals(action))
			{
				spinner.setSelection(i);
				break;
			}
		}
			
		
		event_spinner = (Spinner) findViewById(R.id.event_spinner);
		ArrayAdapter<CharSequence> adapter1 = ArrayAdapter.createFromResource(
				this, R.array.event_array, android.R.layout.simple_spinner_item);
		adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		event_spinner.setAdapter(adapter1);

		for(int i=0;i<event_spinner.getCount();i++)
		{
			if(event_spinner.getItemAtPosition(i).toString().equals(event))
			{
				event_spinner.setSelection(i);
				break;
			}
		}
		
		//event_type=(EditText) findViewById(R.id.event_type_edittext);
		l=(LinearLayout) findViewById(R.id.main);
		final Animation inslide = AnimationUtils.loadAnimation(this, R.anim.lefttoright);
		final Animation slide = AnimationUtils.loadAnimation(this, R.anim.righttoleft);

		l.startAnimation(inslide);
		inslide.setFillAfter(true);
		inslide.setAnimationListener(new AnimationListener(){
			public void onAnimationEnd(Animation animation){

				final Button button1 = (Button) findViewById(R.id.update_rule_btn);
				button1.setOnClickListener(new View.OnClickListener() {
					public void onClick(View v) {
						// Perform action on click
						l.startAnimation(slide);
						slide.setFillAfter(true);
						slide.setAnimationListener(new AnimationListener(){
							public void onAnimationEnd(Animation animation){
								HttpClient httpclient = new DefaultHttpClient();
								//HttpPost httppost = new HttpPost("http://10.0.2.2/update_rule.php");
								HttpPost httppost = new HttpPost("http://192.168.16.1/update_rule.php");
								try {
									// Add your data
									List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(3);
									nameValuePairs.add(new BasicNameValuePair("username", GlobalData.username));
									nameValuePairs.add(new BasicNameValuePair("event_type", event_spinner.getSelectedItem().toString()));
									nameValuePairs.add(new BasicNameValuePair("action", spinner.getSelectedItem().toString()));
									httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

									// Execute HTTP Post Request
									HttpResponse response = httpclient.execute(httppost);
									System.out.println("Response ->"+response.toString());
									//Intent i = new Intent(getBaseContext(),last_screen.class);
									Intent i = new Intent(getBaseContext(),display_rules.class);
									startActivity(i);

								} catch (ClientProtocolException e) {
									System.out.println("exception 1");
									// TODO Auto-generated catch block
								} catch (IOException e) {
									e.printStackTrace();
									System.out.println("error ");
									// TODO Auto-generated catch block
								}

							}

							@Override
							public void onAnimationRepeat(Animation arg0) {
								// TODO Auto-generated method stub

							}

							@Override
							public void onAnimationStart(Animation animation) {
								// TODO Auto-generated method stub

							}
						});
					}
				});

			}

			@Override
			public void onAnimationRepeat(Animation arg0) {
				// TODO Auto-generated method stub

			}

			@Override
			public void onAnimationStart(Animation animation) {
				// TODO Auto-generated method stub

			}
		});
	}
}